/*The language compiler for virtual*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <dos.h>

#define NAMEL 32

char *builtin_syms[]={"declare","integer","=","+","-","/","*","for","to","if",
"then","exit","end","next","call"};

char *lang="\
declare integer i			\n\
declare integer m			\n\
m = read_m 20				\n\
for i = m to 100			\n\
	if event_is = 1 then	\n\
		exit for			\n\
	end if					\n\
next i						\n\
call event_received			\n\
";

enum {SYM_UNDEF,SYM_INT,SYM_STR,SYM_BYTE,SYM_WORD,SYM_DWORD};

typedef struct symbol {
	char name[NAMEL];
	struct symbol *next,*prev;
}sym_t;
sym_t *syms=NULL,*symstail=NULL;

sym_t *new_sym()
{
	sym_t *n;
	n=(sym_t *)malloc(sizeof(sym_t));
	strcpy(n->name,"");
	n->next=n->prev=NULL;
	return n;
}

/*Add a symbol to the linked list above*/
void add_sym(sym_t *n)
{
	if(!syms) {
		syms=n;
		syms->next=syms->prev=NULL;
		symstail=syms;
	} else {
		syms->prev=n;
		n->next=syms;
		n->prev=NULL;
		syms=n;
	}
}

/*Validate a char to check if it's a symbol*/
int valid_symchar(char x)
{
	if((x>='a' && x<='z') ||
		(x>='A' && x<='Z') ||
		(x>='0' && x<='9') ||
		(x=='_') ||
		(x=='/')) {
			return 1;
		}
		return 0;
}

/*A symbol cannot start with a number*/
int valid_symstartchar(char x)
{
	if((x>='a' && x<='z') ||
		(x>='A' && x<='Z') ||
		(x=='_') ||
		(x=='/')) {
			return 1;
		}
		return 0;
}

int valid_all_sym_char(char c)
{
	if(c!=' ' && c!='\t' && c!='\n')
		return 1;
	return 0;
}



void all_syms()
{
	int i,len,sptr=0;
	sym_t *n;
	len=strlen(lang);
	n=new_sym();
	for(i=0;i<len;i++) {
		if(valid_all_sym_char(lang[i])) {
			printf("%c",lang[i]);
		} else {

		}
	}
}

void print_syms()
{
	sym_t *t;
	printf("\nFound syms:");
	for(t=syms;t;t=t->next) {
		printf("\n%s",t->name);
	}
}


int main()
{
	printf("\n-------------------------------------\n");
	all_syms();
	//print_syms();
	getch();
}
